package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Activities;
import company.domain.Educationalarticles;
import company.domain.Rescuestations;
import company.service.EducationalarticlesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EducationalarticlesController {
    @Resource
    EducationalarticlesService educationalarticlesService;

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllEducationalarticles/{page}")
    public R selectAllEducationalarticles(@PathVariable("page") int page,@RequestBody Educationalarticles educationalarticles){
        QueryWrapper<Educationalarticles> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(educationalarticles.getTitle()!="" && !("".equals(educationalarticles.getTitle())),"title",educationalarticles.getTitle());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Educationalarticles> list=this.educationalarticlesService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }
    /*
     * 新增公告
     * */
    @PostMapping("/AddEducationalarticles")
    public R AddEducationalarticles(@RequestBody Educationalarticles educationalarticles){
        this.educationalarticlesService.save(educationalarticles);
        return new R();
    }

    /*
     * 公告删除
     * */
    @RequestMapping("/DelEducationalarticles/{id}")
    public R DelEducationalarticles(@PathVariable("id") int id){
        boolean remove = this.educationalarticlesService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据公告id查询
     * */
    @PostMapping("/selectEducationalarticles")
    public R selectEducationalarticles(@RequestBody Educationalarticles educationalarticles){
        Educationalarticles byId = this.educationalarticlesService.getById(educationalarticles.getArticleid());
        return new R().add("byId",byId);
    }

    /*
     * 修改公告
     * */
    @PostMapping("/uploadEducationalarticles")
    public R uploadEducationalarticles(@RequestBody Educationalarticles educationalarticles){
        this.educationalarticlesService.updateById(educationalarticles);
        return new R();
    }
}
